<?php

namespace app\service\upload;

use app\model\SysSetting;
use app\service\upload\storage\Aliyuncs;
use app\service\upload\storage\Local;
use app\service\upload\storage\Qiniu;
use app\service\upload\storage\Tencent;

class Upload
{
    /**
     * @var array
     */
    private $info;
    private $file;
    private $storage;
    private $seller_id;
    protected $type = ['local'=>1,'aliyun'=>2,'qiniu'=>3,'tencent'=>4];
    /**
     * @throws \app\exception\ModelEmptyException
     * @throws \app\exception\ModelException
     */
    public function create($file, $seller_id, $type = null,$path=null): bool
    {
       $this->file = $file;
       $this->seller_id = $seller_id;
       $this->storage = $this->type[$type];
       // 上传到本地
       if ($type == 'local') {
           $file_save = new Local($path);
           $this->info = $file_save->localSave($file);
           return true;
       }
       // 上传到云端
        $param = $this->getCloudConfig($type);
        if ($type == 'qiniu') {
           $qiniu = new Qiniu();
           $this->info = $qiniu->upload($file, $file->getPathname(), $param);
       } elseif ($type == 'tencent') {
           $tencent = new Tencent();
           $this->info = $tencent->upload($file->getOriginalName(), $file->getPathname(), $param);
       } elseif ($type == 'aliyun') {
           $aliyun = new Aliyuncs();
           $this->info = $aliyun->upload($file->getOriginalName(), $file->getPathname(), $param);
       } else {
           return false;
       }
       return true;
   }

    /**
     * 获取文件相关的基本信息
     * @return array
     */
   public function getUploadFileInfo(): array
   {
       $param['name'] = $this->file->getOriginalName();
       $param['url'] = $this->info['path'];
       $param['mime_type'] = $this->file->getMime();
       $param['storage'] = $this->storage;
       return dataReturn(0, '获取成功', $param);
   }

    /**
     * 获取相对应的云的基本配置信息
     * @param $type
     * @return array
     * @throws \app\exception\ModelEmptyException
     * @throws \app\exception\ModelException
     */
   public function getCloudConfig($type): array
   {
        $where = [
            'seller_id' => $this->seller_id,
            'group' => 'upload',
            'title' => $type,
        ];

        $systemSetting = new SysSetting();
        $infoId = $systemSetting->getSysSetting($where)['data']->toArray()['id'];
        $res = $systemSetting->getAllSysSetting(['parent_id' => $infoId])['data']->toArray();
        foreach ($res as $value) {
            switch ($value['title']) {
                case $type.'_accessKeyId':
                    $data['accessKeyId'] = $value['value'];
                    break;
                case $type.'_accessKeySecret':
                    $data['accessKeySecret'] = $value['value'];
                    break;
                case $type.'_storageRoot':
                    $data['storageRoot'] = $value['value'];
                    break;
                case $type.'_bucket':
                    $data['bucket'] = $value['value'];
                    break;
                case $type.'_protocolHeader':
                    $data['protocolHeader'] = $value['value'];
                    break;
                case $type.'_region':
                    $data['region'] = $value['value'];
                    break;
            }
        }
        return $data;
   }
}